<?php
/**
 * 公司主体信息模型
 */

namespace app\badmin\model;


use app\common\model\BaseModel;
use custom\Err;
use oss\Oss;

class Company extends BaseModel {

    /**
     * 减少剩余可添加经纪人
     * @param $num
     * @param $id
     * @throws \think\Exception
     */
    public static function lessAgentNum($num, $id) {
        return db('company')
            ->where('id', $id)
            ->setDec('lave_num', $num);
    }

    /**
     * 补回剩余可添加经纪人
     * @param $num
     * @param $id
     * @throws \think\Exception
     */
    public static function addAgentNum($num, $id) {
        return db('company')
            ->where('id', $id)
            ->setInc('lave_num', $num);
    }

    /**
     * 公司主体信息
     * @param $params
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getInfo($params) {
        $data = db("company")
            ->where("id", $params['company_id'])
            ->field('f_name,f_phone,name,logo,province,city,area,address,applets_id,appid,secret,applets_code,video,video_cover')
            ->find();
        $data['official'] = 'www.fangzhupai.com';
        return $data;
    }

    /**
     * 编辑
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function edit($params) {
        $old = db("company")->where('deleted', 0)->find();
        if ($params['account_type'] != 0) {
            Err::add('当前账号不是公司账号');
            return false;
        }
        $data = [
            'video'       => $params['video'],
            'video_cover' => $params['video_cover']
        ];
        //删除图片
        if ($old['video'] != $params['video'] && !empty($old['video'])) {
            $oss = Oss::getInstance();
            $oss->delOneFile($old['video']);
        }
        if ($old['video_cover'] != $params['video_cover'] && !empty($old['video_cover'])) {
            $oss = Oss::getInstance();
            $oss->delOneFile($old['video_cover']);
        }
        self::where('id', $params['company_id'])->update($data);
        return true;
    }

    /**
     * 经纪人数统计
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function agentNum($params) {
        $company = db("company")->where('id', $params['company_id'])->field('agent_num,lave_num')->find();
        $useAgentNum = $company['agent_num'] - $company['lave_num'];//已使用的经纪人数
        $lave_num = $company['lave_num']; //未使用经纪人数
        return [
            $lave_num,
            $useAgentNum,
        ];
    }

    /**
     * 获取单条
     * @param $map
     * @param $column
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOne($map, $column) {
        return db("company")
            ->where($map)
            ->field($column)
            ->find();
    }

    public static function getAutoHouseUrl($url, $company_id) {
        return db("company")
            ->where('id', $company_id)
            ->update(['auto_house_url' => $url]);
    }

}